<template>
  <view class="container">
    <view class="example">
      <uni-forms ref="form" :model="purchaseAddForm" :rules="rules" labelWidth="80px">
        <uni-forms-item label="饲料类型" name="materiaFeedType" required>
          <uni-data-select v-model="purchaseAddForm.materiaFeedType" placeholder="请选择饲料类型"
          	:localdata="material_type"
            ></uni-data-select>
        </uni-forms-item>
		<uni-forms-item label="存储地点" name="materiaFeedStorePosition" required>
		  <uni-data-select v-model="purchaseAddForm.materiaFeedStorePosition" placeholder="请选择存储地点"
		  	:localdata="store_position"
		    ></uni-data-select>
		</uni-forms-item>
        <uni-forms-item label="供应商" name="materiaFeedPurchaseProvider" required>
          <uni-easyinput v-model="purchaseAddForm.materiaFeedPurchaseProvider" placeholder="请输入供应商" />
        </uni-forms-item>
		<uni-forms-item label="数量" name="materiaFeedPurchaseNum" required>
		  <uni-easyinput type="number" v-model="purchaseAddForm.materiaFeedPurchaseNum" placeholder="请输入数量(袋)" />
		</uni-forms-item>
		<uni-forms-item label="单价" name="materiaFeedPurchasePrice" required>
		  <uni-easyinput type="number" v-model="purchaseAddForm.materiaFeedPurchasePrice" placeholder="请输入单价" @blur="changeTotlePrice" />
		</uni-forms-item>
		<uni-forms-item label="每袋重量" name="materiaFeedPurchaseWeight" required>
		  <uni-easyinput type="number" v-model="purchaseAddForm.materiaFeedPurchaseWeight" placeholder="请输入每袋重量(kg)" />
		</uni-forms-item>
		<uni-forms-item label="总价" name="materiaFeedPurchaseTotlePrice" required>
		  <uni-easyinput type="number" v-model="purchaseAddForm.materiaFeedPurchaseTotlePrice" placeholder="请输入总价" />
		</uni-forms-item>
		<uni-forms-item label="进货时间" name="materiaFeedPurchaseDate" required>
		  <uni-datetime-picker type="datetime" v-model="purchaseAddForm.materiaFeedPurchaseDate"/>
		</uni-forms-item>
		<uni-forms-item label="过期时间" name="materiaFeedExpireTime" required>
		  <uni-datetime-picker type="datetime" v-model="purchaseAddForm.materiaFeedExpireTime"/>
		</uni-forms-item>
		<uni-forms-item label="备注">
			<uni-easyinput type="textarea" v-model="purchaseAddForm.remark" placeholder="请输入备注" />
		</uni-forms-item>
      </uni-forms>
      <button type="primary" plain="true" @click="submit('form')">提交</button>
    </view>
  </view>
</template>

<script>


import {listMaterial, addPurchase, getPurchase, updatePurchase, delPurchase, getTypes} from "@/api/hmy/material";


export default {
  name: "Purchase",
  //dicts: ['hmy_pig_breed_type'],
  data() {
    return {
		//饲料类型
		material_type:[],
		typesList:[],
		// material_type:[
		// 	{ value: 1, text: '粗饲料'},
		// 	{ value: 2, text: '青绿饲料'},
		// 	{ value: 3, text: '青贮饲料'},
		// 	{ value: 4, text: '能量饲料'},
		// 	{ value: 5, text: '蛋白质饲料'},
		// 	{ value: 6, text: '矿物质饲料'}
		// ],
		store_position:[
			{ text: '一号仓库', value: 1},
			{ text: '二号仓库', value: 2},
			{ text: '三号仓库', value: 3}
		],
		purchaseAddForm: {},
		rules: {
			materiaFeedType: {
				rules: [{required: true, errorMessage: '饲料种类不能为空'}]
			},
			materiaFeedPurchaseDate: {
				rules: [{required: true, errorMessage: '进货时间不可为空'}]
			},
			materiaFeedPurchaseProvider: {
		  		rules: [{required: true, errorMessage: '供应商不能为空'}]
			},
			materiaFeedPurchaseNum: {
				rules: [{required: true, errorMessage: '数量不能为空'}]
			},
			materiaFeedPurchaseWeight: {
				rules: [{required: true, errorMessage: '每袋重量不能为空'}]
			},
			materiaFeedPurchasePrice: {
				rules: [{required: true, errorMessage: '单价不能为空'}]
			},
			materiaFeedPurchaseTotlePrice: {
				rules: [{required: true, errorMessage: '总价不能为空'}]
			},
			materiaFeedStorePosition: {
				rules: [{required: true, errorMessage: '存储地点不能为空'}]
			},
			materiaFeedExpireTime: {
				rules: [{required: true, errorMessage: '过期时间不能为空'}]
			}
	    },
	}
  },
  created(){
	  this.getTypesList();
  },
  methods: {
	  //动态获取饲料数据
	  getTypesList(){
	    getTypes().then(res => {
	  	this.typesList = res.data;
	  	this.typesList.forEach((item,index) => {
	  		this.material_type.push({
	  			'text': item.materiaFeedType,
	  			'value': item.materiaFeedTypeId
	  		})
	  	})
	    })
	  },
    submit(form) {
      this.$refs[form].validate().then(res => {
        addPurchase(this.purchaseAddForm).then(response => {
          this.$modal.msgSuccess("新增成功")
		  setTimeout(()=>{
		  	this.$tab.navigateTo('/pages/hmy/material/purchase');
		  },1000)
		  
        })
      })
    },
	
	//动态总价
	changeTotlePrice(){
		if (this.purchaseAddForm.materiaFeedPurchasePrice != undefined && this.purchaseAddForm.materiaFeedPurchaseNum != undefined){
			this.purchaseAddForm.materiaFeedPurchaseTotlePrice = this.purchaseAddForm.materiaFeedPurchasePrice * this.purchaseAddForm.materiaFeedPurchaseNum;
	    }
	},
  }
}
</script>

<style lang="scss">
	
  /* #ifndef APP-NVUE */
  page {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    min-height: 100%;
    height: auto;
  }

  view {
    font-size: 16px;
    line-height: inherit;
  }
  /* #endif */
  .code{
	margin-bottom :5px;
  }
  .icon{
	float:right;
	margin-right:10px;
	margin-top :5px;
  }
  .message{
	margin-top:5px;
	color: #989897;
	line-height: 25px;
  }

  
  .example {
    padding: 15px;
    background-color: #fff;
  }
  
  .segmented-control {
    margin-bottom: 15px;
  }
  
  .button-group {
    margin-top: 15px;
    display: flex;
    justify-content: space-around;
  }
  
  .form-item {
    display: flex;
    align-items: center;
    flex: 1;
  }
  
  .button {
    display: flex;
    align-items: center;
    height: 35px;
    line-height: 35px;
    margin-left: 10px;
  }
</style>

